﻿using CPrint.bo;
using CPrint.ice;
using FastReport;
using NLog;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Printing;

namespace CPrint
{
    public partial class MainForm : Form
    {
        private static BlockingCollection<PrintObject> queue = new BlockingCollection<PrintObject>();

        private static Logger logger = LogManager.GetCurrentClassLogger();


        public MainForm()
        {
            InitializeComponent();
            Bootstrap bootstrap = new Bootstrap();

            bootstrap.Queue = queue;
            dataGridView1.AllowUserToAddRows = false;
            logger.Info(GetPrintList().ToString());
            printerName.DataSource = GetPrintList();
            printerName.ValueMember = "PrinterName";
            printerName.DataPropertyName = "printerName";
            operatorType.DataSource = getOperatorTypes();
            operatorType.ValueMember = "Value";
            operatorType.DataPropertyName = "Key";

            templateName.DataSource = getTemplates();
            templateName.ValueMember = "Value";
            templateName.DataPropertyName = "Key";
        }

        
        private List<Dict> getTemplates()
        {
            List<Dict> dicts = new List<Dict>();
            dicts.Add(new Dict("", "拣货标签", "拣货标签"));
            dicts.Add(new Dict("", "拣货单", "拣货单"));
            dicts.Add(new Dict("", "热敏面单", "热敏面单"));
            return dicts;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // create report instance
            Report report = new Report();

            report.Load(@"..\..\report.frx");

            logger.Info("123");

            // register the business object
            report.RegisterData(CreateBusinessObject(), "Master");



            // run the report
            report.Print();

            // free resources used by report
            report.Dispose();
        }


        private List<Master> CreateBusinessObject()
        {
            List<Master> masters = new List<Master>();
            Master master = new Master();
            master.Field1 = "M1";
            master.Field2 = "M2";
            Detail detail = new Detail();
            detail.Field1 = "D1";
            detail.Field2 = "D2";
            master.Details.Add(detail);

            Detail detail2 = new Detail();
            detail2.Field1 = "D12";
            detail2.Field2 = "D22";
            master.Details.Add(detail2);

            masters.Add(master);
            return masters;
        }

        private List<Printer> GetPrintList()
        {
            List<Printer> lt = new List<Printer>();
            LocalPrintServer printServer = new LocalPrintServer();
            PrintQueueCollection printQueuesOnLocalServer = printServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local });
            foreach (PrintQueue printer in printQueuesOnLocalServer)
            {
                Printer printerBo = new Printer();
                printerBo.PrinterName = printer.Name;
                printerBo.PrinterValue = printer.Name;
                lt.Add(printerBo);
            }
                
            return lt;
        }

        private List<Dict> getOperatorTypes()
        {
            List<Dict> dicts = new List<Dict>();
            dicts.Add(new Dict("","拣货环节-拣货标签", "拣货环节-拣货标签"));
            dicts.Add(new Dict("", "拣货环节-拣货单", "拣货环节-拣货单"));
            dicts.Add(new Dict("", "波次环节-热敏面单", "波次环节-热敏面单"));
            return dicts;
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            DataGridViewRow dr = new DataGridViewRow();
            dataGridView1.Rows.Insert(0, dr);
        }
    }
}
